package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.model.form.driver.DriverFaceModelForm;
import com.atguigu.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.atguigu.daijia.model.vo.driver.DriverAuthInfoVo;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //微信小程序登录
    @Override
    public String login(String code) {
        //远程调用获取司机id
        Result<Long> result = driverInfoFeignClient.login(code);
//        if (result.getCode() != 200){ //状态码不是200，则调用失败
//            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
//        }
        Long driverId = result.getData();
        if (driverId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //生成token
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //将数据存放在redis中，并设置过期时间
        String key = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
        redisTemplate.opsForValue().set(key,driverId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        return token;
    }

    @Override
    public DriverLoginVo getDriverLoginInfo() {
        //获取当前登录司机id
        Long driverId = AuthContextHolder.getUserId();

        //远程调用获取信息
        Result<DriverLoginVo> driverInfoResult = driverInfoFeignClient.getDriverInfo(driverId);
        if (driverInfoResult.getCode() != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        DriverLoginVo driverLoginVo = driverInfoResult.getData();
        if (driverLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return driverLoginVo;
    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> driverAuthInfo = driverInfoFeignClient.getDriverAuthInfo(driverId);
        DriverAuthInfoVo driverAuthInfoVo = driverAuthInfo.getData();
        return driverAuthInfoVo;
    }

    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm);
        Boolean result = booleanResult.getData();
        return result;
    }


    //创建司机人脸模型
    @Override
    public Boolean createDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.createDriverFaceModel(driverFaceModelForm);
        return booleanResult.getData();
    }
}
